package com.lenovo.smarttraffic.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.lenovo.smarttraffic.Constant;
import com.lenovo.smarttraffic.R;
import com.lenovo.smarttraffic.ui.adapter.BasePagerAdapter;
import com.lenovo.smarttraffic.ui.fragment.FirstFragment;
import com.lenovo.smarttraffic.ui.fragment.SecondFragment;
import com.lenovo.smarttraffic.util.CommonUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * @author Amoly
 * @date 2019/4/11.
 * description：
 */

public class Item1Activity extends BaseActivity {
    @BindView(R.id.tab_layout_list)
    TabLayout tabLayoutList;
    @BindView(R.id.header_layout)
    LinearLayout headerLayout;
    @BindView(R.id.view_pager)
    ViewPager viewPager;
    @BindView(R.id.iv_plus)
    ImageView mImageView;

    private List<Fragment> fragments;
    private ArrayList<String> titles;
    private static final Integer REQUEST_CODE = 1;
    private BasePagerAdapter basePagerAdapter;
    private static final String TAG = "Item1Activity";
    private String[] initArray=new String[]{
            "推荐","热点","科技","汽车资讯"
    };

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        InitView();
        InitData();
    }
    @Override
    protected int getLayout() {
        return R.layout.activity_list_tab;
    }

    private void InitView() {
        initToolBar("交通资讯");
        tabLayoutList.setupWithViewPager(viewPager);
        tabLayoutList.setTabMode(TabLayout.MODE_SCROLLABLE);
        headerLayout.setBackgroundColor(CommonUtil.getInstance().getColor());
        InitData();
        mImageView.setOnClickListener(v -> {
            Intent intent = new Intent(Item1Activity.this, TrafficItemActivity.class);
            //保证来回切换时，item的顺序一定
            ArrayList<String> strings = new ArrayList<>();
            for (int i = 0; i <basePagerAdapter.getCount() ; i++) {
                strings.add(basePagerAdapter.getPageTitle(i).toString());
            }
            intent.putStringArrayListExtra(Constant.EXTRA_ITEM1_TRAFFIC_EXTRA, strings);
            startActivityForResult(intent, REQUEST_CODE);
        });
    }

    private void InitData() {
        //为了第二个界面的分类
        titles = new ArrayList<>();
        fragments = new ArrayList<>();
        for (int i = 0; i <4 ; i++) {
            titles.add(initArray[i]);
            fragments.add(FirstFragment.newInstance(initArray[i]));
//            fragments.add(new SecondFragment());
        }
        basePagerAdapter = new BasePagerAdapter(getSupportFragmentManager(), fragments, titles);
        viewPager.setAdapter(basePagerAdapter);
        viewPager.setOffscreenPageLimit(2);
}

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode != Activity.RESULT_OK) {
            return;
        }
        if (requestCode == REQUEST_CODE) {
            ArrayList<String> resultList = new ArrayList<>();
            resultList.addAll(data.getStringArrayListExtra(Constant.EXTRA_TRAFFIC_INFO));
            if(resultList!=null){
                ArrayList<Fragment> fragments = new ArrayList<>();
                for (int i = 0; i <resultList.size() ; i++) {
                    fragments.add(FirstFragment.newInstance(resultList.get(i)));
                }
                basePagerAdapter.recreateItems(fragments,resultList);
            }

        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        headerLayout.setBackgroundColor(CommonUtil.getInstance().getColor());
    }

}
